﻿<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="TSBUrlShortenerDocuments"
     Location="Microsoft.SharePoint.SiteSettings"
     Title="Short Url Settings"
     Description="Generates a short Url for SharePoint."
     ImageUrl="/_layouts/images/TSB.SharePointUrlShortener/icon16.png"
                GroupId="Customization"
      >
        <UrlAction Url="/_layouts/TSB.SharePointUrlShortener/short-urls.aspx" />
  </CustomAction>

  <CustomAction Id="TSBUrlShortenerDocuments"
    Location="EditControlBlock"
    Title="Generate Short Url"
    Description="Generates a short Url for SharePoint."
    RegistrationType="ContentType"
    RegistrationId="0x0101"
    ImageUrl="/_layouts/images/TSB.SharePointUrlShortener/icon16.png"
  >
    <UrlAction Url="javascript:OpenPopUpPageWithTitle('/_layouts/TSB.SharePointUrlShortener/Url-Shortener-Settings.aspx?Type=Item&amp;ItemUrl={ItemUrl}', null, 650, 300,'Generate Short Url')" />
  </CustomAction>
  
  <CustomAction Id="TSBUrlShortenerDocuments"
    Title="Generate Short Url"
    Description="Generates a short Url for SharePoint."
    RegistrationType="ContentType"
    RegistrationId="0x0120"
    Location="EditControlBlock"
    ImageUrl="/_layouts/images/TSB.SharePointUrlShortener/icon16.png"    
  >
    <UrlAction Url="javascript:OpenPopUpPageWithTitle('/_layouts/TSB.SharePointUrlShortener/Url-Shortener-Settings.aspx?Type=Folder&amp;ItemUrl={ItemUrl}', null, 650, 300,'Generate Short Url')" />
  </CustomAction>

  <CustomAction
    Id="TSB.Ribbon.UrlShortener"
    Location="CommandUI.Ribbon">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.List.Share.Controls._children">
              <Button
                Id="TSB.Ribbon.List.UrlShortener.UrlShortener"
                Command="TSB.Ribbon.UrlShortener.Command.EditPermissions"
                Sequence="15" Image16by16="/_layouts/images/TSB.SharePointUrlShortener/icon16.png" Image32by32="/_layouts/images/TSB.SharePointUrlShortener/icon32.png"
                Description="Short Urls"
                LabelText="Generate Short Url"
                TemplateAlias="o1" />          
        </CommandUIDefinition>
        <CommandUIDefinition Location="Ribbon.Library.Share.Controls._children">
              <Button
                Id="TSB.Ribbon.Library.UrlShortener.UrlShortener"
                Command="TSB.Ribbon.UrlShortener.Command.EditPermissions"
                Sequence="15" Image16by16="/_layouts/images/TSB.SharePointUrlShortener/icon16.png" Image32by32="/_layouts/images/TSB.SharePointUrlShortener/icon32.png"
                Description="Short Urls"
                LabelText="Generate Short Url"
                TemplateAlias="o1" />
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler          
        Command="TSB.Ribbon.UrlShortener.Command.EditPermissions"
        CommandAction="javascript:
        var list;
   
        function onSuccessMethod(sender, args) {
            var options = SP.UI.$create_DialogOptions();
            options.url = '/_layouts/TSB.SharePointUrlShortener/Url-Shortener-Settings.aspx?Type=List&amp;ItemUrl=' + list.get_defaultViewUrl();
            options.width = 600;
            options.height = 300;
            options.dialogReturnValueCallback= Function.createDelegate(null, CloseCallback);
            SP.UI.ModalDialog.showModalDialog(options);
        }
        function onFailureMethod(sender, args) {
            SP.UI.Notify.addNotification('A problem was encountered: ' + args.get_message());
        }
        function CloseCallback(result, target) {
        }         
        function GetList() {
          var context = SP.ClientContext.get_current(); 
          this.web = context.get_web(); 
          var listId = SP.ListOperation.Selection.getSelectedList(); 
          list = this.web.get_lists().getById(listId); 
                
          context.load(list);
          context.executeQueryAsync(Function.createDelegate(null, onSuccessMethod), Function.createDelegate(null, onFailureMethod));
        }
        
        GetList();
        "     />
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>
  
</Elements>